﻿using Script.Audio;
using Script.Framework;
using UnityEngine;
using UnityEngine.UI;

namespace Script.UI._1.Main
{
    public class SetPanel : UIBase
    {
        private Button btnSet;
        private Button btnClose;
        private Text switchT;
        private Text volumeT;
        private Toggle togAudio;
        private Slider sldVolume;
        private Button btnQuit;
        private Image bg;


        // Start is called before the first frame update
        private void Start()
        {
            // 获取对象
            switchT = transform.Find("SwitchT").GetComponent<Text>();
            volumeT = transform.Find("VolumeT").GetComponent<Text>();
            btnSet = transform.Find("Btn_Set").GetComponent<Button>();
            btnClose = transform.Find("Btn_Close").GetComponent<Button>();
            togAudio = transform.Find("Switch").GetComponent<Toggle>();
            sldVolume = transform.Find("Slider").GetComponent<Slider>();
            btnQuit = transform.Find("Btn_Exit").GetComponent<Button>();
            bg = transform.Find("Bg").GetComponent<Image>();

            // 注册事件
            btnSet.onClick.AddListener(setClick);
            btnClose.onClick.AddListener(closeClick);
            btnQuit.onClick.AddListener(exitClick);
            togAudio.onValueChanged.AddListener(togAudio_onValueChanged);
            sldVolume.onValueChanged.AddListener(volumeValueChanged);

            // 初始化
            SetObjectActive(false);
        }


        private void SetObjectActive(bool active)
        {
            bg.gameObject.SetActive(active);
            btnClose.gameObject.SetActive(active);
            btnQuit.gameObject.SetActive(active);
            togAudio.gameObject.SetActive(active);
            sldVolume.gameObject.SetActive(active);
            switchT.gameObject.SetActive(active);
            volumeT.gameObject.SetActive(active);
        }


        private void setClick()
        {
            SetObjectActive(true);
        }


        private void closeClick()
        {
            SetObjectActive(false);
        }

        private void exitClick()
        {
            Application.Quit();
        }

        /// <summary>
        /// 音乐开关
        /// </summary>
        /// <param name="result"></param>
        private void togAudio_onValueChanged(bool result)
        {
            // 操作声音
            if (result)
            {
                Dispatch(AreaCode.AUDIO, AudioEvent.PLAY_BG_AUDIO, null);
            }
            else
            {
                Dispatch(AreaCode.AUDIO, AudioEvent.STOP_BG_AUDIO, null);
            }
        }

        /// <summary>
        /// 音量控制
        /// </summary>
        /// <param name="value"></param>
        private void volumeValueChanged(float value)
        {
            Dispatch(AreaCode.AUDIO, AudioEvent.SET_BG_VOLUME, value);
        }


        public override void OnDestroy()
        {
            base.OnDestroy();

            btnSet.onClick.RemoveListener(setClick);
            btnClose.onClick.RemoveListener(closeClick);
            btnQuit.onClick.RemoveListener(exitClick);
        }
    }
}